//
//  CZNetWorkTool.m
//  NewsBoard
//
//  Created by Apple on 16/4/21.
//  Copyright © 2016年 itcast. All rights reserved.
//

#import "CZNetWorkTool.h"
#import <AFNetworking.h>

@implementation CZNetWorkTool
//baseUrl
static NSString * const NewsBoardBaseURLString = @"http://iosapi.itcast.cn/car/";

static CZNetWorkTool *_instance;
/**
 *  创建单例
 */
+ (instancetype)sharedNetWorkTool{
    
    static dispatch_once_t onceToken;
    
    dispatch_once(&onceToken, ^{
        
        _instance = [[self alloc] initWithBaseURL:[NSURL URLWithString:NewsBoardBaseURLString]];
        
        _instance.responseSerializer.acceptableContentTypes = [_instance.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];
        
    });
    
    return _instance;
}

- (void)objectWithURLString:(NSString *)URLString andParam:(NSDictionary *)parma completeBlock:(CompleteBlock)completeBlock{
    self.requestSerializer = [AFJSONRequestSerializer serializer];
    
    self.responseSerializer = [AFHTTPResponseSerializer serializer];

    [self POST:URLString parameters:parma progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:NULL];
//        NSLog(@"%@",dict);
        if (completeBlock!=nil){
            completeBlock(dict);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",error);
    }];
}

@end
